package com.goods.business.controller;


import com.goods.business.enums.InStockStatusEnum;
import com.goods.business.service.InStockService;
import com.goods.common.error.BusinessException;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/business/inStock")
public class BizInStockController {

    @Autowired
    private InStockService inStockService;

    @GetMapping("findInStockList")
    public ResponseBean<PageVO<InStockVO>> findInStockList(@RequestParam Integer pageNum,
                                                           @RequestParam Integer pageSize,
                                                           InStockVO inStockVO
    ) {
        PageVO<InStockVO> list = inStockService.findInStockList(pageNum, pageSize, inStockVO);
        return ResponseBean.success(list);
    }

    @GetMapping("detail/{id}")
    public ResponseBean<Map<String, Object>> detail(@RequestParam(defaultValue = "1") Integer pageNum,
                                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                                    @PathVariable Integer id) {
        Map<String, Object> res = inStockService.findInStockDetailList(pageNum, pageSize, id);
        return ResponseBean.success(res);
    }

    /**
     * 移到回收站
     *
     * @param id
     * @return
     */
    @PutMapping("remove/{id}")
    public ResponseBean remove(@PathVariable Long id) {
        inStockService.updateInStockStatus(id, InStockStatusEnum.TRASH.getCode());
        return ResponseBean.success();
    }

    /**
     * 删除回收站里的物资
     *
     * @param id
     * @return
     */
    @GetMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        inStockService.deleteInStockById(id);
        return ResponseBean.success();
    }

    /**
     * 恢复
     *
     * @param id
     * @return
     */
    @PutMapping("back/{id}")
    public ResponseBean back(@PathVariable Long id) {
        inStockService.updateInStockStatus(id, InStockStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }

    /**
     * 入库
     *
     * @param inStockVO
     * @return
     */
    @PostMapping("addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO) throws BusinessException {
        inStockService.addIntoStock(inStockVO);
        return ResponseBean.success();
    }

    /**
     * 审核
     *
     * @param id
     * @return
     */
    @PutMapping("publish/{id}")
    public ResponseBean audit(@PathVariable Long id) {
        inStockService.auditInStock(id, InStockStatusEnum.NORMAL.getCode());
        return ResponseBean.success();
    }

}

